package com.wxjfkg.workflow.impl;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.fivestar.credit.service.vo.CreditConstants;
import com.fivestar.customer.service.CustomerService;
import com.fivestar.customer.service.vo.CustomerDto;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fivestar.app.vo.AppConstants;
import com.fivestar.app.vo.AppMessageVO;
import com.fivestar.appmsg.service.AppMsgService;
import com.fivestar.credit.service.ApplyCreditService;
import com.fivestar.credit.service.vo.ApplyCredTakeDto;
import com.fivestar.loan.service.vo.ApplyConstants;
import com.fivestar.merchant.service.MerchantService;
import com.fivestar.merchant.service.vo.MerchantInfoDto;
import com.fivestar.query.vo.ResultDto;
import com.fivestar.util.date.DateTimeUtils;

/**
 * 用信审批通过更新申请状态时间
 * 2017年2月8日 <br/> 
 * 
 * @author zyq 
 * @version
 */
@Service("loanUseApproveSuccessListener")
public class LoanUseApproveSuccessListener{
	public Logger logger = LoggerFactory.getLogger(LoanUseApproveSuccessListener.class);
	@Autowired
	private ApplyCreditService applyCreditService;
	@Autowired
	private AppMsgService appMsgService;
	@Value("${loanUseMsgContent}")
	private String loanUseMsgContent;
	@Autowired
	private MerchantService merchantService;

	@Autowired
	private CustomerService customerService;

	public void notify(ExecutionEntity delegateTask) {
		String bussKey = delegateTask.getBusinessKey();
		try {
			ApplyCredTakeDto creditTake = new ApplyCredTakeDto();
			creditTake.setApplyId(bussKey);
			ResultDto<ApplyCredTakeDto> retTake = applyCreditService.queryApplyCredit(creditTake);
			
			if(retTake.isSuccess() && retTake.getData() !=null){
				creditTake = retTake.getData();
				//生成提醒消息
				AppMessageVO messageVO = new AppMessageVO();
				messageVO.setMsgSubType(AppConstants.APP_MSG_SUBTYPE.WALLET);
				messageVO.setMsgTitle("贷款提醒");
		        //从催收内容模板生成催收内容
				String prodName = "";
				if(CreditConstants.CREDIT_USE_TYPE.JH.equals(creditTake.getLoanUse())){
					prodName = "进货";
				}else if(CreditConstants.CREDIT_USE_TYPE.NQ.equals(creditTake.getLoanUse())){
					prodName = "拿钱";
				}
				//todo 以下编辑消息代码在多处用到待抽成独立方法
				//申请用户名称
				String applicantName = null;
				//申请用户登录账号
				String applicantLoginId = null;
				//店铺
				if(StringUtils.isNotEmpty(creditTake.getMerchId())) {
					MerchantInfoDto merchantInfo = new MerchantInfoDto();
					merchantInfo.setMerchId(creditTake.getMerchId());
					List<MerchantInfoDto> merchantInfoList = merchantService.qryMerchantList(merchantInfo);
					if (merchantInfoList != null && !merchantInfoList.isEmpty()) {
						merchantInfo = merchantInfoList.get(0);
					}
					applicantName = merchantInfo.getMerchName();
					applicantLoginId = merchantInfo.getMerchRelatedMobile();
				}
				//个人
				else if(StringUtils.isNotEmpty(creditTake.getCustId())) {
					CustomerDto customerDto = new CustomerDto();
					customerDto.setCustId(creditTake.getCustId());
					List<CustomerDto> customerDtoList = customerService.qryCustomerList(customerDto);
					if(customerDtoList != null && !customerDtoList.isEmpty()) {
						customerDto = customerDtoList.get(0);
					}
					applicantName = customerDto.getCustName();
					//todo 待确认服务中心后台登录方式
					applicantLoginId = customerDto.getCustId();
				}
		        String content = MessageFormat.format(loanUseMsgContent,
						applicantName, bussKey.substring(bussKey.length() - 4), prodName, "已审批通过");
		        messageVO.setMsgContent(content);
		        messageVO.setContentSource(bussKey);
		        Date now = new Date();
		        //消息有效时间为提醒时间当天至次日
		        messageVO.setStartTime(now);
		        messageVO.setEndTime(DateTimeUtils.getUpdateDateTime(now, Calendar.DAY_OF_MONTH, 1));
		        messageVO.setLoginId(applicantLoginId);
				appMsgService.createXbtBusinessMsg(messageVO);
				
				ApplyCredTakeDto apply = new ApplyCredTakeDto();
				apply.setAppStat(ApplyConstants.DiscountStatusEnum.STATE_SUCCESS.getStatusCode());
				apply.setApplyId(bussKey);
				applyCreditService.mergeApplyCredStat(apply);
			}else{
				logger.error("查询用信申请信息失败"+bussKey);
			}
		} catch (Exception e) {
			logger.error("用信申请状态更新失败{}"+bussKey , e);
		}
		
	}

}
